package cn.wgx.modules.sys.web;

import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.services.SysLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(description = "系统日志管理")
//@RestController
@RequestMapping(value="${allPathPre}syslog")
public class SysLogController extends BaseController {

    @Resource
    SysLogService sysLogService;

    @ApiOperation("获取日志列表")
    @RequestMapping(path = "getlist", method = RequestMethod.GET)
    @RequiresRoles(value = {"admin"})
    public Map<String,Object> getList(@ApiParam("获取指定用户日志") @RequestParam(required = false) Integer userId,
                                      @ApiParam("起始日期") @RequestParam(required = false)Date startDate,
                                      @ApiParam("截止日期") @RequestParam(required = false)Date endDate,
                                      @RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {

        Map<String,Object> map = getInitMap();
        Page p = PageHelper.startPage(page,  Math.min(Math.abs(pageSize), maxPageSize));
        map.put("data", sysLogService.getList(userId, startDate, endDate));
        map.put("total",p.getTotal());
        return map;
    }

    @ApiOperation("删除日志")
    @RequiresRoles(value = {"admin"})
    @RequestMapping(path = "deletebatch", method = RequestMethod.DELETE)
    public Map<String,Object>  deleteBatch(@RequestParam("id") List<Integer> ids) {
        Map<String,Object> map = getInitMap();
        int count = sysLogService.deleteBatch(ids);
        map.put("data", count);
        return map;
    }

}